package DT42;

import javagprssrv.StructClass.structTCPServerSocket;
import javagprssrv.Common.CommonClass;
import java.sql.ResultSet;
import DT8.DT8_Util;
import java.util.Map;
import javagprssrv.DataBase.DBSQLExecute;
import javagprssrv.Common.DateTime;
import javagprssrv.StructClass.structIATask;
import javagprssrv.JGS_Param;
import javagprssrv.StructClass.structTask;
import javagprssrv.StructClass.structTerm;
import javagprssrv.StructClass.structRecvData;
import org.dusin.commonutil.DateUtil;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.params.SetParams;

public class TrdTCPDataTrans_DT42 extends java.lang.Thread
{
    javagprssrv.StructClass.structRecvData recvData;
    javagprssrv.StructClass.structTerm term;
    javagprssrv.StructClass.structTask task;
    int JobIndex;
    DT42.DT42_CommandStruct DT42CMD;
    java.lang.String _Protocol;
    int _UDPServerSocketIndex;
    java.lang.String _RemoteIP;
    int _RemotePort;
    java.lang.String _Data;
    java.lang.String _OriginRecvData;
    int RecvDataType;
    java.lang.String AsynTelNo;
    java.lang.String TaskID;
    java.lang.String ZB;
    java.lang.String TelNO;
    java.lang.String YBH;
    java.lang.String WYM;
    java.lang.String WYM_17;
    java.lang.String TermNO;
    private java.lang.String TCP_TermType;
    Jedis jedis;
    
    public TrdTCPDataTrans_DT42(final javagprssrv.StructClass.structRecvData _recvData) {
        super("TrdTCPDataTrans_DT42");
        this.term = new javagprssrv.StructClass.structTerm();
        this.JobIndex = -1;
        this.DT42CMD = new DT42.DT42_CommandStruct();
        this.RecvDataType = -1;
        this.TCP_TermType = "42";
        this.recvData = _recvData;
        this._Protocol = this.recvData.Protocol.trim();
        this._RemoteIP = this.recvData.RemoteIP;
        this._RemotePort = this.recvData.RemotePort;
        this._Data = this.recvData.Data;
        this._OriginRecvData = this.recvData.Data;
        jedis = new Jedis();
    }
    
    @java.lang.Override
    public void run() {
        try {
            java.lang.Thread.sleep(1000L);
            this.ReadDT42Data();
            final int Data_0 = this._Data.charAt(0);
            if (this.DT42CMD.AFN == 2) {
                if (this.DT42CMD.Fn == 1) {
                    this.do_DT42_XinTiao();
                }
                if (this.DT42CMD.Fn == 3) {
                    this.do_DT42_XinTiao();
                }
            }
            if (this.DT42CMD.AFN == 13) {}
            if (this._Data.charAt(0) == 'Y') {
                this.do_59();
            }
            if (this.DT42CMD.C_DIR == 1) {
                this.locateTask();
                boolean isJSSJ = false;
                try {
                    isJSSJ = this.task.Task_isJSSJ;
                }
                catch (java.lang.Exception ex) {}
                if (isJSSJ) {
                    synchronized (javagprssrv.JGS_Param.IATasks) {
                        for (int idx = javagprssrv.JGS_Param.IATasks.size() - 1; idx >= 0; --idx) {
                            if (((structIATask)javagprssrv.JGS_Param.IATasks.elementAt(idx)).ClientIP.equals(this.task.JSSJ_ClientIP) && ((structIATask)javagprssrv.JGS_Param.IATasks.elementAt(idx)).ClientPort == this.task.JSSJ_ClientPort) {
                            	((structIATask)javagprssrv.JGS_Param.IATasks.elementAt(idx)).RecvData = this._Data;
                                break;
                            }
                        }
                    }
                }
                else {
                    synchronized (javagprssrv.JGS_Param.UnSavedRecvData_TCP) {
                        this.recvData.RecvDataType = java.lang.Integer.valueOf(this.TCP_TermType);
                        javagprssrv.JGS_Param.UnSavedRecvData_TCP.add(this.recvData);
                    }
                }
                final int cmd = this._Data.substring(4, 5).charAt(0);
                if (cmd == 243) {
                    return;
                }
                if (cmd == 244) {
                    return;
                }
                if (cmd == 245) {
                    return;
                }
                this.do_41();
            }
        }
        catch (java.lang.Exception ex2) {}
    }
    
    private void do_DT42_XinTiao() {
        int idx = -1;
        for (int i = 0; i < javagprssrv.JGS_Param.TCP_Terms.size(); ++i) {
            if (javagprssrv.JGS_Param.TCP_Terms.elementAt(i).Term_RemoteIP.equals(this._RemoteIP) && javagprssrv.JGS_Param.TCP_Terms.elementAt(i).Term_RemotePort == this._RemotePort && javagprssrv.JGS_Param.TCP_Terms.elementAt(i).Term_Type.equals(this.TCP_TermType)) {
                idx = i;
                break;
            }
        }
        if (idx <= -1) {
            this.YBH = "DT42";
            this.TelNO = "13800000000";
            this.WYM = this.DT42CMD.A_A1 + "." + java.lang.String.valueOf(this.DT42CMD.A_A2);
            this.term.Term_Type = this.TCP_TermType;
            this.term.Term_SerialNo = this.YBH;
            this.term.Term_TelNo = this.TelNO;
            this.term.Term_WYM = this.WYM;
            this.term.Term_RemoteIP = this._RemoteIP;
            this.term.Term_RemotePort = this._RemotePort;
            synchronized (javagprssrv.JGS_Param.TCP_Terms) {
                for (int j = javagprssrv.JGS_Param.TCP_Terms.size() - 1; j >= 0; --j) {
                    if (javagprssrv.JGS_Param.TCP_Terms.elementAt(j).Term_RemoteIP.equals(this._RemoteIP) && javagprssrv.JGS_Param.TCP_Terms.elementAt(j).Term_RemotePort == this._RemotePort) {
                        javagprssrv.JGS_Param.TCP_Terms.remove(j);
                    }
                    else if (javagprssrv.JGS_Param.TCP_Terms.elementAt(j).Term_WYM.equals(this.WYM)) {
                        javagprssrv.JGS_Param.TCP_Terms.remove(j);
                    }
                }
                javagprssrv.JGS_Param.TCP_Terms.add(this.term);
            }
        }
        java.lang.String CMD = "";
        this.DT42CMD.C_DIR = 0;
        this.DT42CMD.C_PRM = 0;
        this.DT42CMD.C_FCB = 0;
        this.DT42CMD.C_FCV = 0;
        this.DT42CMD.C_FuctionCode = 0;
        this.DT42CMD.AFN = 0;
        this.DT42CMD.SEQ_TpV = 0;
        this.DT42CMD.SEQ_FIR = 1;
        this.DT42CMD.SEQ_FIN = 1;
        this.DT42CMD.SEQ_CON = 0;
        ++this.DT42CMD.SEQ_PSEQ_RSEQ;
        if (this.DT42CMD.SEQ_PSEQ_RSEQ > 15) {
            this.DT42CMD.SEQ_PSEQ_RSEQ = 0;
        }
        this.DT42CMD.Pn = 0;
        this.DT42CMD.Fn = 1;
        this.DT42CMD.Data = "";
        this.DT42CMD.AUX = "";
        CMD = this.DT42CMD.WriteCommand();
        try {
            java.lang.Thread.sleep(500L);
        }
        catch (java.lang.Exception ex) {}
        this.TCPDataSend(this._RemoteIP, this._RemotePort, CMD);
        this.Update_T_Device_FromXinTiao();
    }
    
    public void Update_T_Device_FromXinTiao() {
        try {
            final java.lang.String A1 = java.lang.String.valueOf(this.DT42CMD.A_A1);
            final java.lang.String A2 = java.lang.String.valueOf(this.DT42CMD.A_A2);
            final java.lang.String A3 = java.lang.String.valueOf(this.DT42CMD.A_A3);
            //final java.lang.String Time = new javagprssrv.Common.DateTime().getDateTime("YYYY-MM-DD HH:NN:SS");
            final java.lang.String IP = this.recvData.RemoteIP;
            final java.lang.String Port = java.lang.String.valueOf(this.recvData.RemotePort);
            //final java.lang.String SQL = "UPDATE T_Device set IP='" + IP + "',PORT=" + Port + ",LastTalk='" + Time + "' where AdminAreaCode='" + A1 + "' and TerminalNO='" + A2 + "' and ZBM=" + A3;
            SetParams setParams = new SetParams();
            setParams.ex(40*60L);// 40分钟过期
            String time = DateUtil.get();
            String keys = "hb."+A1+"#"+A2+"#"+A3;
            jedis.set(keys, time,setParams);            
        }
        catch (java.lang.Exception ex) {
            ex.printStackTrace();
        }
    }
    
    public void ReadDT42Data() {
        this.DT42CMD.ReadCommand(this._OriginRecvData);
    }
    
    private java.lang.String getWYM() {
        java.lang.String res = "";
        if (this._Data.length() >= 17) {
            final int offset = 25;
            final int rightOffset = 11;
            res = this._Data.substring(offset, offset + 17);
        }
        return res;
    }
    
    private void do_confirm() {
        final java.lang.String wym = this._Data.substring(8, 25);
        final int zlx = this._Data.charAt(25);
        final int bwlx = this._Data.charAt(26);
        final int zxlh = this._Data.charAt(27);
        final int jcdh = this._Data.charAt(28);
        final int fszt = 255;
        final java.lang.String response = this.MakeCommand(wym, 4, bwlx, zxlh, jcdh, fszt, "");
        this.TCPDataSend(this._RemoteIP, this._RemotePort, response);
    }
    
    private void do_xintiao() {
        final int idx_A55A = this._Data.indexOf("¥Z");
        final int idx_9680 = this._Data.indexOf("\u0096\u0080");
        java.lang.String crcString = this._Data.substring(idx_A55A + 2, idx_9680 - 2);
        crcString = new DT8.DT8_Util().DT8_CRC(crcString);
        final int zlx = this._Data.charAt(25);
        final int bwlx = this._Data.charAt(26);
        final int zxlh = this._Data.charAt(27);
        final int jcdh = this._Data.charAt(28);
        final java.lang.String YBID18 = this._Data.substring(7, 25);
        final java.lang.String YBID19 = this._Data.substring(8, 25);
        this.do_TermLogin(YBID19);
        final java.lang.String SJM = new DT8.DT8_Util().DT8_GetSJM();
        java.lang.String response = "";
        response = "\u0005" + YBID18 + '\u0002' + (char)bwlx + (char)zxlh + 'ÿ' + SJM;
        final java.lang.String CRC = new DT8.DT8_Util().DT8_CRC(response);
        response = "\u0003\u0000\u0000\u0000¥Z" + response + CRC + '\u0096';
        response += new DT8.DT8_Util().DT8_FillCommand(response);
        final int len = response.length();
        response = response.substring(0, 2) + (char)(len / 255) + (char)(len % 255) + response.substring(4);
        this.TCPDataSend(this._RemoteIP, this._RemotePort, response);
    }
    
    //数据库结构不符
    private void do_TermLogin(final java.lang.String _WYM) {
        this.WYM = _WYM;
        int idx = -1;
        for (int i = 0; i < javagprssrv.JGS_Param.TCP_Terms.size(); ++i) {
            if (javagprssrv.JGS_Param.TCP_Terms.elementAt(i).Term_RemoteIP.equals(this._RemoteIP) && javagprssrv.JGS_Param.TCP_Terms.elementAt(i).Term_RemotePort == this._RemotePort && javagprssrv.JGS_Param.TCP_Terms.elementAt(i).Term_Type.equals(this.TCP_TermType)) {
                idx = i;
                break;
            }
        }
        if (idx <= -1) {
            final javagprssrv.DataBase.DBSQLExecute dbse = new javagprssrv.DataBase.DBSQLExecute();
            final java.lang.String _wym11 = this.WYM.substring(this.WYM.length() - 11);
            final java.lang.String sql = "select * from t_yb yb where 唯一码='" + _wym11 + "'";
            final java.sql.ResultSet rs = dbse.SQL_Query(0, sql);
            try {
                rs.next();
                this.YBH = rs.getString("仪表号").trim();
                this.TelNO = rs.getString("手机卡号").trim();
            }
            catch (java.lang.Exception e) {
                javagprssrv.JGS_Param.myLog.ShowLog("[ 系统未登记该仪表 ] = " + sql, java.lang.Boolean.TRUE, java.lang.Boolean.TRUE);
                return;
            }
            this.term.Term_Type = this.TCP_TermType;
            this.term.Term_SerialNo = this.YBH;
            this.term.Term_TelNo = this.TelNO;
            this.term.Term_WYM = this.WYM;
            this.term.Term_RemoteIP = this._RemoteIP;
            this.term.Term_RemotePort = this._RemotePort;
            synchronized (javagprssrv.JGS_Param.TCP_Terms) {
                for (int j = javagprssrv.JGS_Param.TCP_Terms.size() - 1; j >= 0; --j) {
                    if (javagprssrv.JGS_Param.TCP_Terms.elementAt(j).Term_RemoteIP.equals(this._RemoteIP) && javagprssrv.JGS_Param.TCP_Terms.elementAt(j).Term_RemotePort == this._RemotePort) {
                        javagprssrv.JGS_Param.TCP_Terms.remove(j);
                    }
                    else if (javagprssrv.JGS_Param.TCP_Terms.elementAt(j).Term_WYM.equals(this.WYM)) {
                        javagprssrv.JGS_Param.TCP_Terms.remove(j);
                    }
                }
                javagprssrv.JGS_Param.TCP_Terms.add(this.term);
            }
        }
    }
    
    private void do_denglu() {
        int zxh = this._Data.charAt(7) + '\u0001';
        zxh %= 255;
        final java.lang.String res = "\u0001\u0005\u0000\u0016\u0000" + (char)zxh + '\0' + '\u0001' + '\u0002' + '\u0003' + '\u0004' + '\u0005' + '\u0006' + '\u0007' + '\b' + '\t' + '\n' + '\u000b' + '\f' + '\r' + '\u000e' + '\u000f';
        this.TCPDataSend(this._RemoteIP, this._RemotePort, res);
    }
    
    private void locateTask() {
        int idxTask = -1;
        final java.lang.String ip = this.recvData.RemoteIP;
        final java.lang.String port = java.lang.String.valueOf(this.recvData.RemotePort);
        for (int i = 0; i < javagprssrv.JGS_Param.Tasks.size(); ++i) {
            if (javagprssrv.JGS_Param.Tasks.elementAt(i).Task_State == 2) {
                if (javagprssrv.JGS_Param.Tasks.elementAt(i).BindGPRS_IP.equals(ip) && javagprssrv.JGS_Param.Tasks.elementAt(i).BindGPRS_Port.equals(port)) {
                    idxTask = i;
                    this.TaskID = javagprssrv.JGS_Param.Tasks.elementAt(i)._TaskID;
                    this.task = javagprssrv.JGS_Param.Tasks.elementAt(i);
                    break;
                }
            }
        }
    }
    
    private void do_41() throws java.lang.Exception {
        int idxTask = -1;
        final java.lang.String ip = this.recvData.RemoteIP;
        final java.lang.String port = java.lang.String.valueOf(this.recvData.RemotePort);
        for (int i = 0; i < javagprssrv.JGS_Param.Tasks.size(); ++i) {
            if (javagprssrv.JGS_Param.Tasks.elementAt(i).Task_State == 2) {
                if (javagprssrv.JGS_Param.Tasks.elementAt(i).BindGPRS_IP.equals(ip) && javagprssrv.JGS_Param.Tasks.elementAt(i).BindGPRS_Port.equals(port)) {
                    idxTask = i;
                    this.TaskID = javagprssrv.JGS_Param.Tasks.elementAt(i)._TaskID;
                    this.task = javagprssrv.JGS_Param.Tasks.elementAt(i);
                    this.JobIndex = javagprssrv.JGS_Param.Tasks.elementAt(i).Task_JobsIndex + 1;
                    ++javagprssrv.JGS_Param.Tasks.elementAt(i).Task_JobsIndex;
                    javagprssrv.JGS_Param.Tasks.elementAt(i).Task_StartDateTime = new javagprssrv.Common.DateTime().getDateTime("YYYY-MM-DD HH:NN:SS");
                    if (this.JobIndex >= javagprssrv.JGS_Param.Tasks.elementAt(i).Task_Jobs.size()) {
                        javagprssrv.JGS_Param.Tasks.elementAt(i).Task_StartDateTime = "2000-01-01 01:02:03";
                        break;
                    }
                    break;
                }
            }
        }
        if (idxTask == -1) {
            return;
        }
        this.do_nextJob();
    }
    
    private void do_59() throws java.lang.Exception {
        this.AsynTelNo = this._Data.substring(1, this._Data.length() - 1);
        int idxTask = -1;
        for (int i = 0; i < javagprssrv.JGS_Param.Tasks.size(); ++i) {
            if (javagprssrv.JGS_Param.Tasks.elementAt(i).Task_State == 1) {
                if (javagprssrv.JGS_Param.Tasks.elementAt(i).TaskAsynTelNO.equals(this.AsynTelNo)) {
                    idxTask = i;
                    this.TaskID = javagprssrv.JGS_Param.Tasks.elementAt(i)._TaskID;
                    this.task = javagprssrv.JGS_Param.Tasks.elementAt(i);
                    this.WYM = this.task.TaskAsynTelNO_ALL;
                    this.JobIndex = javagprssrv.JGS_Param.Tasks.elementAt(i).Task_JobsIndex + 1;
                    ++javagprssrv.JGS_Param.Tasks.elementAt(i).Task_JobsIndex;
                    javagprssrv.JGS_Param.Tasks.elementAt(i).Task_StartDateTime = new javagprssrv.Common.DateTime().getDateTime("YYYY-MM-DD HH:NN:SS");
                    javagprssrv.JGS_Param.Tasks.elementAt(i).BindGPRS_IP = this.recvData.RemoteIP;
                    javagprssrv.JGS_Param.Tasks.elementAt(i).BindGPRS_Port = java.lang.String.valueOf(this.recvData.RemotePort);
                    javagprssrv.JGS_Param.Tasks.elementAt(i).Task_State = 2;
                    break;
                }
            }
        }
        if (idxTask == -1) {
            return;
        }
        if (this.DT42CMD.A_A1 == null) {
            this.DT42CMD.A_A1 = this.AsynTelNo.substring(0, 4);
            this.DT42CMD.A_A2 = java.lang.Integer.valueOf(this.AsynTelNo.substring(5));
            this.DT42CMD.A_A3 = 0;
            try {
                //final java.lang.String sql = "select * from V_DeviceExtend where AdminAreaCode='" + this.DT42CMD.A_A1 + "' and TerminalNO=" + this.DT42CMD.A_A2;
                final javagprssrv.DataBase.DBSQLExecute dbse = new javagprssrv.DataBase.DBSQLExecute();
                Map<String,Object> map = dbse.findYbInfo(this.DT42CMD.A_A1, ""+this.DT42CMD.A_A2);
                this.DT42CMD.A_A3 = (Short)map.get("zbm");              
            }
            catch (java.lang.Exception ex) {}
        }
        this.do_nextJob();
    }
    
    private void do_nextJob() throws java.lang.Exception {
        java.lang.String job = this.task.Task_Jobs.elementAt(this.JobIndex).trim().toUpperCase();
        job += "                ";
        if (job.substring(0, 8).equals("GET DATE")) {
            this.do_getDate(job.trim());
        }
        if (job.substring(0, 9).equals("GET MONTH")) {
            this.do_getMonth(job.trim());
        }
        if (job.substring(0, 9).equals("GET PARAM")) {
            this.do_getParam(job.trim());
        }
        if (job.substring(0, 5).equals("RESET")) {
            this.do_reset(job.trim());
        }
        if (job.substring(0, 8).equals("GET TIME")) {
            this.do_getTime(job.trim());
        }
        if (job.substring(0, 8).equals("SET TIME")) {
            this.do_setTime(job.trim());
        }
        if (job.substring(0, 7).equals("GET SXX")) {
            this.do_getSXX(job.trim());
        }
        if (job.substring(0, 7).equals("SET SXX")) {
            this.do_setSXX(job.trim());
        }
        if (job.substring(0, 8).equals("SET DATE")) {
            this.do_setDate(job.trim());
        }
        if (job.substring(0, 9).equals("SET MONTH")) {
            this.do_setMonth(job.trim());
        }
    }
    
    private java.lang.String MakeCommand(final java.lang.String _WYM_17, final int _ZLX, final int _BWLX, final int _ZXLH, final int _JCDH, final int _QXSJLX, final java.lang.String _CMD) {
        java.lang.String res = "";
        res = "\u0000" + _WYM_17 + (char)_ZLX + (char)_BWLX + (char)_ZXLH + (char)_JCDH + (char)_QXSJLX + _CMD;
        final int cmdLength = 2 + _CMD.length();
        res = "" + (char)cmdLength + res;
        final java.lang.String CRC = new DT8.DT8_Util().DT8_CRC(res);
        res = "\u0003\u0000\u0000\u0000¥Z" + res + CRC + '\u0096';
        res += new DT8.DT8_Util().DT8_FillCommand(res);
        final int len = res.length();
        res = res.substring(0, 2) + (char)(len / 255) + (char)(len % 255) + res.substring(4);
        return res;
    }
    
    private void do_getTime(java.lang.String job) throws java.lang.Exception {
        try {
            java.lang.Thread.sleep(500L);
        }
        catch (java.lang.Exception ex) {}
        job = job.toUpperCase();
        if (!job.substring(0, 8).equals("GET TIME")) {
            return;
        }
        job = job.trim();
        java.lang.String CMD = "";
        this.DT42CMD.C_DIR = 0;
        this.DT42CMD.C_PRM = 1;
        this.DT42CMD.C_FCB = 0;
        this.DT42CMD.C_FCV = 0;
        this.DT42CMD.C_FuctionCode = 10;
        this.DT42CMD.AFN = 10;
        this.DT42CMD.SEQ_TpV = 0;
        this.DT42CMD.SEQ_FIR = 1;
        this.DT42CMD.SEQ_FIN = 1;
        this.DT42CMD.SEQ_CON = 0;
        ++this.DT42CMD.SEQ_PSEQ_RSEQ;
        if (this.DT42CMD.SEQ_PSEQ_RSEQ > 15) {
            this.DT42CMD.SEQ_PSEQ_RSEQ = 0;
        }
        this.DT42CMD.Pn = 0;
        this.DT42CMD.Fn = 53;
        this.DT42CMD.Data = "";
        this.DT42CMD.AUX = "";
        CMD = this.DT42CMD.WriteCommand();
        this.TCPDataSend(this._RemoteIP, this._RemotePort, CMD);
    }
    
    private void do_setTime(java.lang.String job) throws java.lang.Exception {
        try {
            java.lang.Thread.sleep(500L);
        }
        catch (java.lang.Exception ex) {}
        job = job.toUpperCase();
        if (!job.substring(0, 8).equals("SET TIME")) {
            return;
        }
        job = job.trim();
        final java.lang.String time = job.substring(9, 21);
        final java.lang.String yy = new javagprssrv.Common.CommonClass()._2BCD(java.lang.Integer.valueOf(time.substring(0, 2)));
        final java.lang.String mm = new javagprssrv.Common.CommonClass()._2BCD(java.lang.Integer.valueOf(time.substring(2, 4)));
        final java.lang.String dd = new javagprssrv.Common.CommonClass()._2BCD(java.lang.Integer.valueOf(time.substring(4, 6)));
        final java.lang.String hh = new javagprssrv.Common.CommonClass()._2BCD(java.lang.Integer.valueOf(time.substring(6, 8)));
        final java.lang.String nn = new javagprssrv.Common.CommonClass()._2BCD(java.lang.Integer.valueOf(time.substring(8, 10)));
        final java.lang.String ss = new javagprssrv.Common.CommonClass()._2BCD(java.lang.Integer.valueOf(time.substring(10, 12)));
        java.lang.String CMD = "";
        this.DT42CMD.C_DIR = 0;
        this.DT42CMD.C_PRM = 1;
        this.DT42CMD.C_FCB = 0;
        this.DT42CMD.C_FCV = 0;
        this.DT42CMD.C_FuctionCode = 10;
        this.DT42CMD.AFN = 4;
        this.DT42CMD.SEQ_TpV = 0;
        this.DT42CMD.SEQ_FIR = 1;
        this.DT42CMD.SEQ_FIN = 1;
        this.DT42CMD.SEQ_CON = 0;
        ++this.DT42CMD.SEQ_PSEQ_RSEQ;
        if (this.DT42CMD.SEQ_PSEQ_RSEQ > 15) {
            this.DT42CMD.SEQ_PSEQ_RSEQ = 0;
        }
        this.DT42CMD.Pn = 0;
        this.DT42CMD.Fn = 53;
        this.DT42CMD.Data = "" + ss + nn + hh + dd + mm + yy;
        this.DT42CMD.AUX = "";
        CMD = this.DT42CMD.WriteCommand();
        this.TCPDataSend(this._RemoteIP, this._RemotePort, CMD);
    }
    
    private void do_reset(java.lang.String job) throws java.lang.Exception {
        try {
            java.lang.Thread.sleep(500L);
        }
        catch (java.lang.Exception ex) {}
        job = job.toUpperCase();
        if (!job.substring(0, 5).equals("RESET")) {
            return;
        }
        job = job.trim();
        java.lang.String CMD = "";
        this.DT42CMD.C_DIR = 0;
        this.DT42CMD.C_PRM = 0;
        this.DT42CMD.C_FCB = 0;
        this.DT42CMD.C_FCV = 0;
        this.DT42CMD.C_FuctionCode = 10;
        this.DT42CMD.AFN = 1;
        this.DT42CMD.SEQ_TpV = 0;
        this.DT42CMD.SEQ_FIR = 1;
        this.DT42CMD.SEQ_FIN = 1;
        this.DT42CMD.SEQ_CON = 1;
        ++this.DT42CMD.SEQ_PSEQ_RSEQ;
        if (this.DT42CMD.SEQ_PSEQ_RSEQ > 15) {
            this.DT42CMD.SEQ_PSEQ_RSEQ = 0;
        }
        this.DT42CMD.Pn = 0;
        this.DT42CMD.Fn = 1;
        this.DT42CMD.Data = "";
        this.DT42CMD.AUX = "";
        CMD = this.DT42CMD.WriteCommand();
        this.TCPDataSend(this._RemoteIP, this._RemotePort, CMD);
    }
    
    private void do_getSXX(java.lang.String job) throws java.lang.Exception {
        try {
            java.lang.Thread.sleep(500L);
        }
        catch (java.lang.Exception ex) {}
        job = job.toUpperCase();
        if (!job.substring(0, 7).equals("GET SXX")) {
            return;
        }
        job = job.trim();
        final java.lang.String LuShu = job.substring(7, 8);
        java.lang.String CMD = "";
        this.DT42CMD.C_DIR = 0;
        this.DT42CMD.C_PRM = 1;
        this.DT42CMD.C_FCB = 0;
        this.DT42CMD.C_FCV = 0;
        this.DT42CMD.C_FuctionCode = 10;
        this.DT42CMD.AFN = 10;
        this.DT42CMD.SEQ_TpV = 0;
        this.DT42CMD.SEQ_FIR = 1;
        this.DT42CMD.SEQ_FIN = 1;
        this.DT42CMD.SEQ_CON = 0;
        ++this.DT42CMD.SEQ_PSEQ_RSEQ;
        if (this.DT42CMD.SEQ_PSEQ_RSEQ > 15) {
            this.DT42CMD.SEQ_PSEQ_RSEQ = 0;
        }
        this.DT42CMD.Pn = java.lang.Integer.valueOf(LuShu);
        this.DT42CMD.Fn = 34;
        this.DT42CMD.Data = "";
        this.DT42CMD.AUX = "";
        CMD = this.DT42CMD.WriteCommand();
        this.TCPDataSend(this._RemoteIP, this._RemotePort, CMD);
    }
    
    public java.lang.String Hex2String(java.lang.String HexString) {
        java.lang.String ret = "";
        HexString = HexString.trim().toUpperCase();
        for (int i = 0; i < HexString.length(); ++i, ++i) {
            int v = -1;
            final java.lang.String s = HexString.substring(i, i + 2);
            v = 16 * new javagprssrv.Common.CommonClass().HexChar2Int(s.charAt(0)) + new javagprssrv.Common.CommonClass().HexChar2Int(s.charAt(1));
            ret += (char)v;
        }
        return ret;
    }
    
    private void do_setSXX(java.lang.String job) throws java.lang.Exception {
        try {
            java.lang.Thread.sleep(500L);
        }
        catch (java.lang.Exception ex) {}
        job = job.toUpperCase();
        if (!job.substring(0, 7).equals("SET SXX")) {
            return;
        }
        job = job.trim();
        final java.lang.String LuShu = job.substring(7, 8);
        final java.lang.String cmd_sxx = job.substring(9).trim();
        java.lang.String CMD = "";
        CMD += (char)(int)java.lang.Integer.valueOf(cmd_sxx.substring(4, 6), 16);
        CMD += (char)(int)java.lang.Integer.valueOf(cmd_sxx.substring(2, 4), 16);
        CMD += (char)(int)java.lang.Integer.valueOf(cmd_sxx.substring(0, 2), 16);
        CMD += (char)(int)java.lang.Integer.valueOf(cmd_sxx.substring(10, 12), 16);
        CMD += (char)(int)java.lang.Integer.valueOf(cmd_sxx.substring(8, 10), 16);
        CMD += (char)(int)java.lang.Integer.valueOf(cmd_sxx.substring(6, 8), 16);
        CMD += (char)(int)java.lang.Integer.valueOf(cmd_sxx.substring(12, 14), 16);
        CMD += (char)(int)java.lang.Integer.valueOf(cmd_sxx.substring(14, 16), 16);
        CMD += (char)(int)java.lang.Integer.valueOf(cmd_sxx.substring(16, 18), 16);
        CMD += (char)(int)java.lang.Integer.valueOf(cmd_sxx.substring(18, 20), 16);
        CMD += (char)(int)java.lang.Integer.valueOf(cmd_sxx.substring(20, 22), 16);
        CMD += (char)(int)java.lang.Integer.valueOf(cmd_sxx.substring(22, 24), 16);
        CMD = CMD + '\0' + '\0' + '\0' + '\0' + '\0' + '\0' + '\0' + '\0' + '\0' + '\0' + '\0' + '\0' + '\0' + '\0' + '\0' + '\0';
        this.DT42CMD.C_DIR = 0;
        this.DT42CMD.C_PRM = 1;
        this.DT42CMD.C_FCB = 0;
        this.DT42CMD.C_FCV = 0;
        this.DT42CMD.C_FuctionCode = 10;
        this.DT42CMD.AFN = 4;
        this.DT42CMD.SEQ_TpV = 0;
        this.DT42CMD.SEQ_FIR = 1;
        this.DT42CMD.SEQ_FIN = 1;
        this.DT42CMD.SEQ_CON = 0;
        ++this.DT42CMD.SEQ_PSEQ_RSEQ;
        if (this.DT42CMD.SEQ_PSEQ_RSEQ > 15) {
            this.DT42CMD.SEQ_PSEQ_RSEQ = 0;
        }
        this.DT42CMD.Pn = java.lang.Integer.valueOf(LuShu);
        this.DT42CMD.Fn = 34;
        this.DT42CMD.Data = CMD;
        this.DT42CMD.AUX = "";
        CMD = this.DT42CMD.WriteCommand();
        this.TCPDataSend(this._RemoteIP, this._RemotePort, CMD);
    }
    
    private void do_getDate(java.lang.String job) throws java.lang.Exception {
        try {
            java.lang.Thread.sleep(500L);
        }
        catch (java.lang.Exception ex) {}
        job = job.toUpperCase();
        if (!job.substring(0, 8).equals("GET DATE")) {
            return;
        }
        job = job.trim();
        final java.lang.String LuShu = job.substring(8, 9);
        final java.lang.String date = job.substring(10, 14);
        final java.lang.String Year = new javagprssrv.Common.DateTime().getDateTime("YYYYMMDDHHNNSS").substring(0, 4);
        java.lang.String CMD = "";
        this.DT42CMD.C_DIR = 0;
        this.DT42CMD.C_PRM = 1;
        this.DT42CMD.C_FCB = 0;
        this.DT42CMD.C_FCV = 0;
        this.DT42CMD.C_FuctionCode = 10;
        this.DT42CMD.AFN = 13;
        this.DT42CMD.SEQ_TpV = 0;
        this.DT42CMD.SEQ_FIR = 1;
        this.DT42CMD.SEQ_FIN = 1;
        this.DT42CMD.SEQ_CON = 0;
        ++this.DT42CMD.SEQ_PSEQ_RSEQ;
        if (this.DT42CMD.SEQ_PSEQ_RSEQ > 15) {
            this.DT42CMD.SEQ_PSEQ_RSEQ = 0;
        }
        this.DT42CMD.Pn = java.lang.Integer.valueOf(LuShu);
        this.DT42CMD.Fn = 33;
        this.DT42CMD.Data = "" + new javagprssrv.Common.CommonClass()._2BCD(java.lang.Integer.valueOf(date.substring(2, 4))) + new javagprssrv.Common.CommonClass()._2BCD(java.lang.Integer.valueOf(date.substring(0, 2))) + new javagprssrv.Common.CommonClass()._2BCD(java.lang.Integer.valueOf(Year.substring(2, 4)));
        this.DT42CMD.AUX = "";
        CMD = this.DT42CMD.WriteCommand();
        this.TCPDataSend(this._RemoteIP, this._RemotePort, CMD);
    }
    
    private void do_setDate(java.lang.String job) throws java.lang.Exception {
        try {
            java.lang.Thread.sleep(500L);
        }
        catch (java.lang.Exception ex) {}
        job = job.toUpperCase();
        if (!job.substring(0, 8).equals("SET DATE")) {
            return;
        }
        job = job.trim();
        final java.lang.String LuShu = job.substring(8, 9);
        final java.lang.String CMD_Data = job.substring(10).trim();
        final java.lang.String[] V = CMD_Data.split(" ");
        final java.lang.String date = V[0];
        final java.lang.String zyxs = V[1];
        final java.lang.String csxs = V[2];
        final java.lang.String cxxs = V[3];
        final java.lang.String csxc = V[4];
        final java.lang.String cxxc = V[5];
        final java.lang.String gzs = V[6];
        final java.lang.String zdz = V[7];
        final java.lang.String zdzs = V[8];
        final java.lang.String zxz = V[9];
        final java.lang.String zxzs = V[10];
        final java.lang.String pjz = V[11];
        final java.lang.String Year = date.substring(0, 4);
        java.lang.String CMD = "";
        this.DT42CMD.C_DIR = 0;
        this.DT42CMD.C_PRM = 1;
        this.DT42CMD.C_FCB = 0;
        this.DT42CMD.C_FCV = 0;
        this.DT42CMD.C_FuctionCode = 10;
        this.DT42CMD.AFN = 19;
        this.DT42CMD.SEQ_TpV = 0;
        this.DT42CMD.SEQ_FIR = 1;
        this.DT42CMD.SEQ_FIN = 1;
        this.DT42CMD.SEQ_CON = 0;
        ++this.DT42CMD.SEQ_PSEQ_RSEQ;
        if (this.DT42CMD.SEQ_PSEQ_RSEQ > 15) {
            this.DT42CMD.SEQ_PSEQ_RSEQ = 0;
        }
        this.DT42CMD.Pn = java.lang.Integer.valueOf(LuShu);
        this.DT42CMD.Fn = 33;
        this.DT42CMD.Data = "" + new javagprssrv.Common.CommonClass()._2BCD(java.lang.Integer.valueOf(date.substring(6, 8))) + new javagprssrv.Common.CommonClass()._2BCD(java.lang.Integer.valueOf(date.substring(4, 6))) + new javagprssrv.Common.CommonClass()._2BCD(java.lang.Integer.valueOf(Year.substring(2, 4)));
        this.DT42CMD.Data = this.DT42CMD.Data + new javagprssrv.Common.CommonClass().int2Bin(java.lang.Integer.valueOf(zyxs)) + new javagprssrv.Common.CommonClass().int2Bin(java.lang.Integer.valueOf(gzs)) + new javagprssrv.Common.CommonClass().int2Bin(java.lang.Integer.valueOf(csxs)) + new javagprssrv.Common.CommonClass().int2Bin(java.lang.Integer.valueOf(cxxs));
        this.DT42CMD.Data = this.DT42CMD.Data + (char)(int)java.lang.Integer.valueOf(csxc) + (char)(int)java.lang.Integer.valueOf(cxxc) + new javagprssrv.Common.CommonClass().int2Bin(java.lang.Integer.valueOf(zyxs) - java.lang.Integer.valueOf(csxs) - java.lang.Integer.valueOf(cxxs));
        this.DT42CMD.Data += new javagprssrv.Common.CommonClass().doubleDY2A07(java.lang.Double.valueOf(zdz));
        this.DT42CMD.Data = this.DT42CMD.Data + new javagprssrv.Common.CommonClass()._2BCD(java.lang.Integer.valueOf(zdzs.substring(10, 12))) + new javagprssrv.Common.CommonClass()._2BCD(java.lang.Integer.valueOf(zdzs.substring(8, 10))) + new javagprssrv.Common.CommonClass()._2BCD(java.lang.Integer.valueOf(zdzs.substring(6, 8)));
        this.DT42CMD.Data += new javagprssrv.Common.CommonClass().doubleDY2A07(java.lang.Double.valueOf(zxz));
        this.DT42CMD.Data = this.DT42CMD.Data + new javagprssrv.Common.CommonClass()._2BCD(java.lang.Integer.valueOf(zxzs.substring(10, 12))) + new javagprssrv.Common.CommonClass()._2BCD(java.lang.Integer.valueOf(zxzs.substring(8, 10))) + new javagprssrv.Common.CommonClass()._2BCD(java.lang.Integer.valueOf(zxzs.substring(6, 8)));
        this.DT42CMD.Data += new javagprssrv.Common.CommonClass().doubleDY2A07(java.lang.Double.valueOf(pjz));
        this.DT42CMD.AUX = "";
        CMD = this.DT42CMD.WriteCommand();
        this.TCPDataSend(this._RemoteIP, this._RemotePort, CMD);
    }
    
    private void do_getParam(java.lang.String job) throws java.lang.Exception {
        try {
            java.lang.Thread.sleep(500L);
        }
        catch (java.lang.Exception ex) {}
        job = job.toUpperCase();
        if (!job.substring(0, 9).equals("GET PARAM")) {
            return;
        }
        job = job.trim();
        java.lang.String Fn = "";
        Fn = job.substring(10);
        java.lang.String CMD = "";
        this.DT42CMD.C_DIR = 0;
        this.DT42CMD.C_PRM = 1;
        this.DT42CMD.C_FCB = 0;
        this.DT42CMD.C_FCV = 0;
        this.DT42CMD.C_FuctionCode = 10;
        this.DT42CMD.AFN = 10;
        this.DT42CMD.SEQ_TpV = 0;
        this.DT42CMD.SEQ_FIR = 1;
        this.DT42CMD.SEQ_FIN = 1;
        this.DT42CMD.SEQ_CON = 0;
        ++this.DT42CMD.SEQ_PSEQ_RSEQ;
        if (this.DT42CMD.SEQ_PSEQ_RSEQ > 15) {
            this.DT42CMD.SEQ_PSEQ_RSEQ = 0;
        }
        this.DT42CMD.Pn = 1;
        this.DT42CMD.Fn = java.lang.Integer.valueOf(Fn);
        this.DT42CMD.Data = "";
        this.DT42CMD.AUX = "";
        CMD = this.DT42CMD.WriteCommand();
        this.TCPDataSend(this._RemoteIP, this._RemotePort, CMD);
    }
    
    private void do_getMonth(java.lang.String job) throws java.lang.Exception {
        try {
            java.lang.Thread.sleep(500L);
        }
        catch (java.lang.Exception ex) {}
        job = job.toUpperCase();
        if (!job.substring(0, 9).equals("GET MONTH")) {
            return;
        }
        job = job.trim();
        final java.lang.String LuShu = job.substring(9, 10);
        final java.lang.String month = job.substring(11, 13);
        final java.lang.String Year = new javagprssrv.Common.DateTime().getDateTime("YYYYMMDDHHNNSS").substring(0, 4);
        java.lang.String CMD = "";
        this.DT42CMD.C_DIR = 0;
        this.DT42CMD.C_PRM = 1;
        this.DT42CMD.C_FCB = 0;
        this.DT42CMD.C_FCV = 0;
        this.DT42CMD.C_FuctionCode = 10;
        this.DT42CMD.AFN = 13;
        this.DT42CMD.SEQ_TpV = 0;
        this.DT42CMD.SEQ_FIR = 1;
        this.DT42CMD.SEQ_FIN = 1;
        this.DT42CMD.SEQ_CON = 0;
        ++this.DT42CMD.SEQ_PSEQ_RSEQ;
        if (this.DT42CMD.SEQ_PSEQ_RSEQ > 15) {
            this.DT42CMD.SEQ_PSEQ_RSEQ = 0;
        }
        this.DT42CMD.Pn = java.lang.Integer.valueOf(LuShu);
        this.DT42CMD.Fn = 36;
        this.DT42CMD.Data = "" + new javagprssrv.Common.CommonClass()._2BCD(java.lang.Integer.valueOf(month.substring(0, 2))) + new javagprssrv.Common.CommonClass()._2BCD(java.lang.Integer.valueOf(Year.substring(2, 4)));
        this.DT42CMD.AUX = "";
        CMD = this.DT42CMD.WriteCommand();
        this.TCPDataSend(this._RemoteIP, this._RemotePort, CMD);
    }
    
    private void do_setMonth(java.lang.String job) throws java.lang.Exception {
        try {
            java.lang.Thread.sleep(500L);
        }
        catch (java.lang.Exception ex) {}
        job = job.toUpperCase();
        if (!job.substring(0, 9).equals("SET MONTH")) {
            return;
        }
        job = job.trim();
        final java.lang.String LuShu = job.substring(9, 10);
        final java.lang.String CMD_Data = job.substring(11).trim();
        final java.lang.String[] V = CMD_Data.split(" ");
        final java.lang.String date = V[0];
        final java.lang.String zyxs = V[1];
        final java.lang.String csxs = V[2];
        final java.lang.String cxxs = V[3];
        final java.lang.String csxc = V[4];
        final java.lang.String cxxc = V[5];
        final java.lang.String gzs = V[6];
        final java.lang.String zdz = V[7];
        final java.lang.String zdzs = V[8];
        final java.lang.String zxz = V[9];
        final java.lang.String zxzs = V[10];
        final java.lang.String pjz = V[11];
        final java.lang.String Year = date.substring(0, 4);
        java.lang.String CMD = "";
        this.DT42CMD.C_DIR = 0;
        this.DT42CMD.C_PRM = 1;
        this.DT42CMD.C_FCB = 0;
        this.DT42CMD.C_FCV = 0;
        this.DT42CMD.C_FuctionCode = 10;
        this.DT42CMD.AFN = 19;
        this.DT42CMD.SEQ_TpV = 0;
        this.DT42CMD.SEQ_FIR = 1;
        this.DT42CMD.SEQ_FIN = 1;
        this.DT42CMD.SEQ_CON = 0;
        ++this.DT42CMD.SEQ_PSEQ_RSEQ;
        if (this.DT42CMD.SEQ_PSEQ_RSEQ > 15) {
            this.DT42CMD.SEQ_PSEQ_RSEQ = 0;
        }
        this.DT42CMD.Pn = java.lang.Integer.valueOf(LuShu);
        this.DT42CMD.Fn = 36;
        this.DT42CMD.Data = "" + new javagprssrv.Common.CommonClass()._2BCD(java.lang.Integer.valueOf(date.substring(4, 6))) + new javagprssrv.Common.CommonClass()._2BCD(java.lang.Integer.valueOf(Year.substring(2, 4)));
        this.DT42CMD.Data = this.DT42CMD.Data + new javagprssrv.Common.CommonClass().int2Bin(java.lang.Integer.valueOf(zyxs)) + new javagprssrv.Common.CommonClass().int2Bin(java.lang.Integer.valueOf(gzs)) + new javagprssrv.Common.CommonClass().int2Bin(java.lang.Integer.valueOf(csxs)) + new javagprssrv.Common.CommonClass().int2Bin(java.lang.Integer.valueOf(cxxs));
        this.DT42CMD.Data = this.DT42CMD.Data + (char)(int)java.lang.Integer.valueOf(csxc) + '\0' + (char)(int)java.lang.Integer.valueOf(cxxc) + '\0' + new javagprssrv.Common.CommonClass().int2Bin(java.lang.Integer.valueOf(zyxs) - java.lang.Integer.valueOf(csxs) - java.lang.Integer.valueOf(cxxs));
        this.DT42CMD.Data += new javagprssrv.Common.CommonClass().doubleDY2A07(java.lang.Double.valueOf(zdz));
        this.DT42CMD.Data = this.DT42CMD.Data + new javagprssrv.Common.CommonClass()._2BCD(java.lang.Integer.valueOf(zdzs.substring(10, 12))) + new javagprssrv.Common.CommonClass()._2BCD(java.lang.Integer.valueOf(zdzs.substring(8, 10))) + new javagprssrv.Common.CommonClass()._2BCD(java.lang.Integer.valueOf(zdzs.substring(6, 8)));
        this.DT42CMD.Data += new javagprssrv.Common.CommonClass().doubleDY2A07(java.lang.Double.valueOf(zxz));
        this.DT42CMD.Data = this.DT42CMD.Data + new javagprssrv.Common.CommonClass()._2BCD(java.lang.Integer.valueOf(zxzs.substring(10, 12))) + new javagprssrv.Common.CommonClass()._2BCD(java.lang.Integer.valueOf(zxzs.substring(8, 10))) + new javagprssrv.Common.CommonClass()._2BCD(java.lang.Integer.valueOf(zxzs.substring(6, 8)));
        this.DT42CMD.Data += new javagprssrv.Common.CommonClass().doubleDY2A07(java.lang.Double.valueOf(pjz));
        this.DT42CMD.AUX = "";
        CMD = this.DT42CMD.WriteCommand();
        this.TCPDataSend(this._RemoteIP, this._RemotePort, CMD);
    }
    
    private void TCPDataSend(final java.lang.String RemoteIP, final int RemotePort, final java.lang.String strSend) {
        int idx = -1;
        for (int i = javagprssrv.JGS_Param.TCPServerSockets.size() - 1; i >= 0; --i) {
            if (((structTCPServerSocket)javagprssrv.JGS_Param.TCPServerSockets.elementAt(i)).RemoteIP.equals(RemoteIP) && ((structTCPServerSocket)javagprssrv.JGS_Param.TCPServerSockets.elementAt(i)).RemotePort == RemotePort) {
                idx = i;
                break;
            }
        }
        if (idx > -1) {
        	((structTCPServerSocket)javagprssrv.JGS_Param.TCPServerSockets.elementAt(idx)).SendData(strSend);
        }
    }
}
